<template>
<div>
  <el-form :model="ruleForm" style="width: 400px;margin: auto" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="昵称" prop="username">
      <el-input type="text" v-model="ruleForm.username" autocomplete="off" @change="findByName()"></el-input><span style="color: red">{{err}}</span>
    </el-form-item>
    <el-form-item label="密码" prop="pass">
      <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="uPic">
      <el-upload style="float: right"
                 class="avatar-uploader"
                 action="http://localhost:7000/live-live/anchor/upload01"
                 :show-file-list="false"
                 :on-success="handleAvatarSuccess"
                 :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl"  class="avatar" >
        <i v-else class=" avatar-uploader-icon"><el-button round style="color: blue">上传图片</el-button></i>
      </el-upload>
      <el-image
        style="width: 100px; height: 100px;float: left"
        :src="ruleForm.uPic"
        fit="fill"></el-image>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
  <img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2374659957,1171743083&fm=26&gp=0.jpg" style="width: 200px;height: 200px">
  <img src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3624645712,2584928633&fm=26&gp=0.jpg" style="width: 200px;height: 200px" >
</div>
</template>

<script>
import axios from 'axios'
axios.defaults.withCredentials=true
export default {
  name: "personal",
  data(){
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      err: '',
      bool: true,
      imageUrl:'',
      ruleForm: {
        pass: '',
        username: '',
        checkPass:'',
        uPic:'',
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],

      }
    };
  },
  methods:{
    handleAvatarSuccess(res, file) {
      this.ruleForm.uPic=res.data;
      this.imageUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    findByName(){
      axios.get("http://localhost:7000/live-user/user/checkName?name="+this.ruleForm.username).then(res=>{
        if(res.data.code==200){
          this.err=res.data.message;
        }else{
          this.err="";
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post("http://localhost:7000/live-user/user/updateUser",{
            uName:this.ruleForm.username,
            uPassword:this.ruleForm.pass,
            uPic:this.ruleForm.uPic
          }).then(res=>{
            if(res.data.code==200){
              this.$cookie.delete("token");
              localStorage.removeItem("data");
               window.location.reload();
               alert("修改成功,请重新登录");
            }else{
              alert(res.data.message);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
    },
  created() {
    var data=JSON.parse( localStorage.getItem("data"));
    this.ruleForm.uPic=data.pic;
  }

}
</script>

<style scoped>

</style>
